'use strict';

const Controller = require('egg').Controller;

class DologinController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = 'hi, dologin';
        // console.log(ctx.request.body);
        var { username, password } = ctx.request.body;
        var db = this.app.mongodb;
        const collection = await db.collection("user");
        var res = await collection.find({ username, password }).toArray();
        var nickname = await collection.find({ username }).toArray();
        // console.log(res);
        if (res.length) {
            ctx.body = {
                status: 200,
                msg: "登录成功"
            }
        } else {
            if (!nickname.length) {
                ctx.body = {
                    status: 400,
                    msg: "用户名未注册，请先注册"
                }
            } else {
                ctx.body = {
                    status: 400,
                    msg: "用户名或密码错误"
                }
            }
        }
    }
}

module.exports = DologinController;
